package com.yj.oa.mapper;

import com.yj.oa.entity.system.OADepartmentConfig;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/18
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface OADepartmentConfigMapper {

    @Insert("INSERT INTO oa_department_config (department_id, department_name, superior_department_id, sequence) " +
            "VALUES(#{item.department_id}, #{item.department_name}, #{item.superior_department_id}, #{item.sequence}) " +
            "ON DUPLICATE KEY " +
            "UPDATE department_id = #{item.department_id}, department_name = #{item.department_name}, " +
            "superior_department_id = #{item.superior_department_id}, sequence = #{item.sequence}")
    void insertUpdate(@Param("item") OADepartmentConfig item);

    @Delete("<script>" +
            "DELETE FROM oa_department_config " +
            "WHERE department_id IN " +
            "<foreach item = 'item' collection = 'list' open = '(' separator = ', ' close = ') '>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    void batchDelete(@Param("list") List<Long> list);

    @Select("SELECT * FROM oa_department_config where department_id = #{department_id}")
    OADepartmentConfig getByDepartmentID(@Param("department_id") Long department_id);

    @Select("SELECT * FROM oa_department_config " +
            "WHERE superior_department_id = #{department_id} AND department_name = \'总经办\' " +
            "LIMIT 1")
    OADepartmentConfig getAuthorityDepartment(@Param("department_id") Long department_id);

    @Select("SELECT * FROM oa_department_config " +
            "WHERE superior_department_id = #{department_id}")
    List<OADepartmentConfig> getSubDepartment(@Param("department_id") Long department_id);

    @Select("SELECT * FROM oa_department_config " +
            "WHERE superior_department_id = #{superior_department_id} AND department_id != #{department_id} " +
            "ORDER BY sequence;")
    List<OADepartmentConfig> subDepartmentForLeader(@Param("superior_department_id") Long superior_department_id,
                                                    @Param("department_id") Long department_id);
}
